#!/bin/bash

echo "🔨 重新编译（无 curl 依赖）"
echo "================================"

# 清理
rm -rf bin .objs
mkdir -p bin .objs

# 编译设置 - 明确使用 NO_CURL
INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-std=c++11 -DAICODER_ENHANCED -m64 -DNO_CURL"

echo "📝 编译核心文件..."

# 只编译必要的核心文件
compile_core() {
    echo "🔸 编译 AI 核心文件:"
    g++ $INCLUDES $CXXFLAGS -c "src/ai_model_config.cpp" -o ".objs/ai_model_config.o" && echo "  ✅ ai_model_config.cpp"
    g++ $INCLUDES $CXXFLAGS -c "src/unified_ai_client.cpp" -o ".objs/unified_ai_client.o" && echo "  ✅ unified_ai_client.cpp"
    g++ $INCLUDES $CXXFLAGS -c "src/aicoder_enhancements.cpp" -o ".objs/aicoder_enhancements.o" && echo "  ✅ aicoder_enhancements.cpp"
    
    echo ""
    echo "🔸 编译 cbp2make 主程序:"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/cbp2make.cpp" -o ".objs/cbp2make.o" && echo "  ✅ cbp2make.cpp"
    
    echo ""
    echo "🔸 编译必要的依赖文件:"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/lib/stringhash.cpp" -o ".objs/stringhash.o" && echo "  ✅ stringhash.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/depsearch.cpp" -o ".objs/depsearch.o" && echo "  ✅ depsearch.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/cbproject.cpp" -o ".objs/cbproject.o" && echo "  ✅ cbproject.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/cbworkspace.cpp" -o ".objs/cbworkspace.o" && echo "  ✅ cbworkspace.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/makefile.cpp" -o ".objs/makefile.o" && echo "  ✅ makefile.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/src/platforms.cpp" -o ".objs/platforms.o" && echo "  ✅ platforms.cpp"
    
    echo ""
    echo "🔸 编译基础库文件:"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/lib/stlstrings.cpp" -o ".objs/stlstrings.o" && echo "  ✅ stlstrings.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/lib/stlconfig.cpp" -o ".objs/stlconfig.o" && echo "  ✅ stlconfig.cpp"
    g++ $INCLUDES $CXXFLAGS -c "../cbp2make/lib/tinyxml/tinyxml.cpp" -o ".objs/tinyxml.o" && echo "  ✅ tinyxml.cpp"
}

compile_core

# 链接
echo ""
echo "🔗 链接可执行文件..."
g++ -m64 .objs/*.o -o bin/cbp2make_ai

if [ $? -eq 0 ]; then
    echo ""
    echo "🎉 编译成功!"
    echo "📋 构建信息:"
    echo "   - 对象文件: $(find .objs -name "*.o" | wc -l) 个"
    echo "   - 可执行文件: bin/cbp2make_ai"
    echo "   - 编译模式: 模拟模式 (NO_CURL)"
else
    echo "❌ 链接失败"
    exit 1
fi
